package com.bawei.detail.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseExpandableListAdapter
import com.bawei.common.entity.CommendEntity
import com.bawei.common.entity.ReplyCommendEntity
import com.bawei.detail.databinding.CommendChildBinding
import com.bawei.detail.databinding.CommendGroupBinding
import com.bawei.image.ImageOptions
import com.bawei.image.ImageStrategy
import com.bawei.image.ImgLoader

/**
 * 自定义可扩展的评论列表
 */
class CommendListAdapter(private val list: MutableList<CommendEntity>): BaseExpandableListAdapter() {

    private var imgLoader: ImgLoader = ImgLoader.Builder.setImageStrategy(ImageStrategy.GLIDE).build()

    //圆形图片参数
    private var imgCircleOption: ImageOptions = ImageOptions.Builder
        .setImageSize(intArrayOf(40, 40))
        .setCircleImage(true)
        .setErrorImage(com.bawei.common.R.drawable.ic_logo)
        .setPlaceImage(com.bawei.common.R.drawable.ic_logo)
        .skipMemoryCache(true)
        .build()

    /**
     * 获取组的个数
     */
    override fun getGroupCount(): Int = list.size

    /**
     * 获取组内子组件个数
     */
    override fun getChildrenCount(groupPosition: Int): Int = list[groupPosition].replyList.size

    /**
     * 获取组的数据实体对象
     */
    override fun getGroup(groupPosition: Int): CommendEntity = list[groupPosition]

    /**
     * 获取组内子组件的数据实体对象
     */
    override fun getChild(groupPosition: Int, childPosition: Int): ReplyCommendEntity = list[groupPosition].replyList[childPosition]

    /**
     * 获取组ID
     */
    override fun getGroupId(groupPosition: Int): Long = list[groupPosition].id.toLong()

    /**
     * 获取子组件ID
     */
    override fun getChildId(groupPosition: Int, childPosition: Int): Long = childPosition.toLong()

    /**
     * 是否有稳定的ID
     */
    override fun hasStableIds(): Boolean = true

    /**
     * 子组件是否可以选中
     */
    override fun isChildSelectable(groupPosition: Int, childPosition: Int): Boolean = true

    /**
     * 获取组的UI组件
     */
    override fun getGroupView(groupPosition: Int, isExpanded: Boolean, convertView: View?, parent: ViewGroup?): View {
        var groupEntity = list[groupPosition]
        val groupDataBinding = CommendGroupBinding.inflate(LayoutInflater.from(parent?.context), parent, false)

        var userIconImageView = groupDataBinding.ivUserIcon
        imgLoader.loadUrlIntoImageView(groupDataBinding.root.context, groupEntity.userlogo, userIconImageView, imgCircleOption)
        groupDataBinding.tvNickName.text = if (groupEntity.nickname.isNullOrEmpty()) "爱唱歌的小孩" else groupEntity.nickname
        groupDataBinding.tvCommend.text = groupEntity.content
        groupDataBinding.tvCreateTime.text = "发布时间：${groupEntity.createtime}"
        groupDataBinding.tvExpand.text = if(isExpanded) "——收起回复——" else "——展开回复——"

        return  groupDataBinding.root
    }

    /**
     * 获取子组件UI组件
     */
    override fun getChildView(groupPosition: Int, childPosition: Int, isLastChild: Boolean, convertView: View?, parent: ViewGroup?): View {
        var childEntity = list[groupPosition].replyList[childPosition]
        val childDataBinding = CommendChildBinding.inflate(LayoutInflater.from(parent?.context), parent, false)

        var userIconImageView = childDataBinding.ivUserIcon
        imgLoader.loadUrlIntoImageView(childDataBinding.root.context, childEntity.userlogo, userIconImageView, imgCircleOption)
        childDataBinding.tvNickName.text = if (childEntity.nickname.isNullOrEmpty()) "爱唱歌的小孩" else childEntity.nickname
        childDataBinding.tvCommend.text = childEntity.content
        childDataBinding.tvCreateTime.text = "回复时间：${childEntity.createtime}"

        return childDataBinding.root
    }

}